<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Validates dates against the format "Y-m-d H:i:s".
 * 
 * @category   Nordic
 * @package    Nordic_Validate
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Datetime.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Validate_Datetime extends Zend_Validate_Abstract
{
    /**
     * Array of validation failure messages
     *
     * @var array
     */
    protected $_messages = array();

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is a valid date of the format YYYY-MM-DD
     *
     * @param  mixed $value
     * @return boolean
     */
    public function isValid($value)
    {
        $this->_messages = array();

        if (!preg_match('/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $value)) {
            $this->_messages[] = "'$value' is not of the format YYYY-MM-DD HH:ii:ss";
            return false;
        }

        list($year, $month, $day, $hour, $minute, $second) = sscanf($value, '%d-%d-%d %d:%d:%d');
		
        if (   !checkdate($month, $day, $year)
        	|| $hour < 0 || $hour > 24
        	|| $minute < 0 || $minute > 60
        	|| $second < 0 || $second > 60) {
            $this->_messages[] = "'$value' does not appear to be a valid date";
            return false;
        }

        return true;
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns array of validation failure messages
     *
     * @return array
     */
    public function getMessages()
    {
        return $this->_messages;
    }
}
